// pages/xuexitong/presign/presign.js
let videoAd = null
const {
    xxtUrl,
    javaUrl,
    qiangguoUrl
} = require("../../../utils/util")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        address: '',
        latitude: '',
        longitude: '',
        time: 1,
        maxtime: 5,
        interval: 60,
        phone: '',
        email: '',
        preSignCount: 0, //预签到机会
    },
    onChange(event) {
        console.log('steped', event.detail);
        this.setData({
            time: parseInt(event.detail)
        })
    },
    timeChange(event) {
        console.log('steped', event.detail);
        this.setData({
            time: parseInt(event.detail)
        })
    },
    intervalChange(event) {
        console.log('steped', event.detail);
        this.setData({
            interval: parseInt(event.detail)
        })
    },


    /*
     *获得预签机会
     *    
     */

    getPreSign() {
        wx.navigateTo({
            url: '../preSignCount/preSignCount',
        })
    },
    presign: function () {
        var that = this
        if (that.data.time == 0) {
            wx.showToast({
                title: '请选择持续时间',
                icon: 'error'
            })
            return
        }
        if (that.data.email == '') {
            wx.showToast({
                title: '请输入邮箱',
                icon: 'error'
            })
            return
        }
        wx.showLoading({
            title: '正在加载',
        })
        wx.request({
            url: 'https://qwer.lovegcu.xyz/prexxt/getactivity/',
            // url: 'http://127.0.0.1:8000/prexxt/getactivity/',
            method: 'POST',
            header: {
                'content-type': 'text/plain'
            },
            data: {
                uf: that.data.user.uf,
                _d: that.data.user._d,
                vc3: that.data.user.vc3,
                uid: that.data.user.UID,
                fid: that.data.user.fid,
                name: that.data.login.realname,
                email: that.data.email,
                address: that.data.address,
                lat: that.data.latitude,
                lon: that.data.longitude,
                minutes: that.data.time * 60,
                interval: that.data.interval
                // uf: "94ffe74515793f367ff3ed06a922c83cb64264624b062c64f46e93bac3aed238ea78bce6210260dbe349f741d6d04bb2dc436c455fddffc288b83130e7eb47043d60939e5db592aa18f170f325985cefc992d031135b2ab7d4a170a1f077b2435e64297a4bf5945faa2ebad65cd196bb",
                // _d: "1670514753653",
                // vc3: "awkx085mIDoDQJkX7OlHrm3dyh5Wqez0D7KHxIKnVjhN%2F1sScCK%2Fb2niZWgpOnliq6nYoPLbaN3QN0vgLsxl5tlpEtJoT4cUTY9%2Bh7%2BuyFiN3V9EcVXb5iSTUTAhYzYseOP1R0nwIH76CfbNOs3xq5YjXJPGNUQDosghr4phWQE%3D8df48aebdaf0e0c2fdee415b078952bf",
                // uid: "114384832",
                // fid:"30155",
                // name:"李光耀",
                // email:"2728622296@qq.com",
                // address:"东莞理工北门",
                // lat: "23.058407",
                // lon:"113.778037",
                // minutes:5
            },
            success(res) {
                console.log("presign", res)
                //减去 presionCount 
                var count = 0;
                count = that.data.preSignCount - that.data.time
                wx.setStorageSync('preSignCount', count)
                that.setData({
                    preSignCount: count
                })
                wx.hideLoading({})
                wx.showToast({
                    title: '成功',
                    icon: 'success'
                })
            }
        })

    },

    bindKeyInput: function (e) {
        this.setData({
            email: e.detail.value
        })
    },

    presignstop() {

    },
    getLocation: function () {
        var _this = this;
        wx.chooseLocation({
            success: function (res) {
                var name = res.name
                var address = res.address
                var latitude = res.latitude
                var longitude = res.longitude
                _this.setData({
                    name: name,
                    address: address,
                    latitude: latitude + 0.006277,
                    longitude: longitude + 0.006621
                })
            },
            complete(r) {
                console.log(r)
                console.log(222)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            user: wx.getStorageSync('signup'),
            phone: wx.getStorageSync('phone'),
            password: wx.getStorageSync('pwd'),
            login: wx.getStorageSync('login'),
            preSignCount: wx.getStorageSync('preSignCount')
        })
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.record']) {
                    wx.authorize({
                        scope: 'scope.userLocation',
                        success() {
                            // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                        }
                    })
                }
            }
        })
        var that = this
        wx.request({
            url: qiangguoUrl + '/login',
            method: 'POST',
            data: {
                phone: that.data.phone,
                password: that.data.password
            },
            success(res) {
                console.log("denglu", res)
                wx.setStorageSync('signup', res.data)
                that.setData({
                    user: res.data,

                })

            }

        })
        // console.log(wx.getStorageSync('preSignCount'),'-----')
        // if(wx.getStorageSync('preSignCount')==''){
        //     wx.setStorageSync('preSignCount', 0)
        // }

        // //在页面onLoad回调事件中创建激励视频广告实例
        // if (wx.createRewardedVideoAd) {
        //     videoAd = wx.createRewardedVideoAd({
        //         adUnitId: 'adunit-d3b7a32c349f2b19'
        //     })
        //     videoAd.onLoad(() => {
        //         console.log('onLoad event emit')
        //     })
        //     videoAd.onError((err) => {
        //         console.log('onError event emit', err)
        //     })
        //     videoAd.onClose((res) => {
        //         console.log('onClose event emit', res)

        //         if (res.isEnded) {
        //             console.log("看完了 ")
        //            count  = wx.getStorageSync('preSignCount')+3
        //             wx.setStorageSync('preSignCount', count)
        //             that.setData({
        //                 preSignCount:count
        //             })
        //             console.log("preSignCount",wx.getStorageSync('preSignCount'));
        //         } else {
        //             console.log("isEnded", res.isEnded)
        //         }
        //     })
        // }

        // // 用户触发广告后，显示激励视频广告
        // if (videoAd) {
        //     videoAd.show().catch(() => {
        //         // 失败重试
        //         videoAd.load()
        //             .then(() => videoAd.show())
        //             .catch(err => {
        //                 console.log('激励视频 广告显示失败')
        //             })
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            preSignCount: wx.getStorageSync('preSignCount')
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})